package com.tools.commons.listenter;

import com.tools.commons.config.properties.SwaggerProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.lang.NonNull;

@Slf4j
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class StartedListener implements ApplicationListener<ApplicationStartedEvent> {
    SwaggerProperties swaggerProperties;

    StartedListener(SwaggerProperties swaggerProperties) {
        this.swaggerProperties = swaggerProperties;
    }

    @Value("${server.port:}")
    private String port;

    @Override
    public void onApplicationEvent(@NonNull ApplicationStartedEvent event) {
        this.printStartInfo();
    }

    private void printStartInfo() {
        String domain = "http://127.0.0.1:" + port;
        log.info("Tools started at              {}", domain);
        if (swaggerProperties.isEnable()) {
            log.debug("Tools api doc was enabled at  {}/swagger/index.html", domain);
        }
        log.info("Tools has started successfully!");
    }

}
